package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/category")
@Slf4j
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    //分页查询
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize){
        //创建分页查询
       Page<Category> pageInfo = new Page<>(page,pageSize);

        //创建条件查询
        LambdaQueryWrapper<Category> lqw = new LambdaQueryWrapper<>();
        lqw.orderByDesc(Category::getUpdateTime);

        //分页查询
        categoryService.page(pageInfo,lqw);

        return R.success(pageInfo);
    }


    //新增菜品
    @PostMapping
    public R<String> save(@RequestBody Category category){
        log.info("正在新增菜品");
        categoryService.save(category);

        return R.success("新增分类成功");
    }

    //修改新增菜品
    @PutMapping
    public R<String> update(@RequestBody Category category){
        log.info("修改"+category);
        category.setType(1);
        //修改
        categoryService.updateById(category);
        return R.success("修改成功");
    }

    //删除新增菜品
    @DeleteMapping
    public R<String> deleteById(Long ids){
        log.info("传入的id{}"+ids);

        categoryService.remove(ids);

        return R.success("删除成功");
    }




}
